function getStyle(obj, attr) {
  'use strict';
  return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
}

/*
  元素移动
  obj 要移动的对象
  direction 要移动的方向，如top，left等
  speed 移动速度
  limit 极限边界值
  freq 频率
  endFn 回调函数
*/
function doMove(obj, direction, speed, limit, freq, endFn) {
  'use strict';
  speed = parseInt(getStyle(obj, direction)) < limit ? speed : -speed;
  clearInterval(obj.moveTimer);
  obj.moveTimer = setInterval(function () {
    var target = parseInt(getStyle(obj, direction)) + speed;
    if ((target > limit && speed > 0) || (target < limit && speed < 0)) {
      target = limit;
    }
    obj.style[direction] = target + 'px';
    if (target === limit) {
      clearInterval(obj.moveTimer);
      if (endFn) endFn();
    }
  }, freq);
}

/*
  obj 要抖动的对象
  direction 要抖动的方向，如top，left等
  endFn 回调函数
*/
function doShake(obj, direction, endFn) {
  'use strict';
  if (typeof obj.timer === 'undefined') {
    var poi = parseInt(getStyle(obj, direction));
    var arr = [];
    var i = 0;
    for (var j = 0; j < 10; j++) {
      arr.push(j, -j);
    }
    obj.timer = setInterval(function () {
      if (i === arr.length) {
        obj.style[direction] = poi + 'px';
        clearInterval(obj.timer);
        delete obj.timer;
        if (endFn) {
          endFn();
        }
      }
      obj.style[direction] = poi + arr[i] + 'px';
      i++;
    }, 20);
  }
}
